<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/27
 * Time: 14:06
 */

namespace app\admin\controller;

use app\reuse\controller\ResponseJson;
use app\service\CostService;
use think\Db;
use think\facade\Session;
class Supplierpayment extends Common
{
    use ResponseJson;

    function __construct()
    {
        parent::__construct();
    }
    /*
     * @param null
     * @content 供应商结款列表
     * @return array
     * */
    function index(){
        if(input("get.active") == "ajax"){
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'),true);
            $_where = "";
            if (!empty($searchParams)) {
                # 商品名称 货号搜索
                if (!empty($searchParams['search'])) {
                    $_where .= "supplier_name like '%" . $searchParams['search'] . "%'
                    or relation_phone like '%" . $searchParams['search'] . "%'
                   ";
                }
            }

            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('supplier_fee sf')
                ->field("s.supplier_name,s.relation_phone,s.account_name,s.bank_account,s.open_bank,s.bank_address,
                (select title from bizpro_class where id in (s.bizpro_class)) bizpro_class,
                sf.*")
                ->join("supplier s",'sf.supplier_id = s.id')
                ->order("id desc")
                ->where("status = 1")
                ->where($_where)
                ->paginate($listRows, false, ['query' => request()->param()]);
            $info =$info->toArray();

            foreach ($info['data'] as &$v){
                $v['pay_price']=PriceNumberFormat($v['pay_price'],2);
                $v['total_price']=PriceNumberFormat($v['total_price'],2);
                $title_arr=Db::name('supplier_class')->alias('sc')->leftJoin(['bizpro_class'=>'bc'],'sc.class_id=bc.id')
                    ->where('sc.id='.$v['supplier_id'])->column('bc.title');
                $v['bizpro_class']=count($title_arr)>0?join(',',$title_arr):'';

//                $class_arr=Db::name('supplier_class')->where('supplier_id='.$v['id'])->column('class_id');
//                $class_titlearr=Db::name('bizpro_class')->where(['id','in',$class_arr])->column('title');
            }

            //dump($info);exit;
            return $this->jsonSuccessData($info);
        }else{
            return $this->fetch();
        }
    }
    /*
    * @param null
    * @content 供应商结款
    * @return array
    * */
    function payindex(){
        if(input("get.active") == "ajax"){
            #供应商结款表的id  supplier_fee
            $id = input("post.id");
            #结款金额
            $pay_money = input("post.pay_money");
            $info = input("post.");
            $info['create_time'] = date("Y-m-d H:i:s");
            #时间
            $time = date("Y-m-d");
            $info['sfee_id'] = $id;
            unset($info['id'],$info['pay_price']);

            #添加结款记录
            $pay = Db::table("log_fee")->insert($info);
            if($pay){
                #自动在资金池内扣除 加记录   减金额
                Db::table("log_poll")->insert(array("create_time"=>date("Y-m-d H:i:s"),'price'=>$pay_money,'pay_type'=>$info['type'],"desc"=>'供应商结款'));
                if($info['type'] == 1){
                    #微信金额
                    Db::table("poll")->where("id = 1")->setDec("wx_price",$pay_money);

                }else{
                    #支付宝金额
                    Db::table("poll")->where("id = 1")->setDec("alipay_price",$pay_money);

                }

                #修改结款时间
                Db::table("supplier_fee")->where("id = $id")->update(array("pay_time"=>date("Y-m-d H:i:s")));
                #修改 累计结款 递增
                Db::table("supplier_fee")->where("id = $id")->setInc("total_pay",$pay_money);
                #结款次数+1
                Db::table("supplier_fee")->where("id = $id")->setInc("pay_number",1);
                #减掉结款金额
                Db::table("supplier_fee")->where("id = $id")->setDec("pay_price",$pay_money);
                #加费用记录 log_cost
                Db::table("log_cost")->insert(array("title"=>'供应商结款金额','explain'=>'供应商结款金额','price'=>$pay_money,'create_time'=>date("Y-m-d H:i:s")));
                #加收支统计 log_income
                Db::table("log_income")->insert(array("pay"=>$info['type'],'desc'=>'供应商结款金额','total_price'=>$pay_money,'create_time'=>date("Y-m-d H:i:s")));

                #判断 利润记录是否有当天的记录  有 则 金额 递增  没有 新加一条
                $profits = Db::table("log_profits")->where("create_time  = '$time'")->find();
                if(!empty($profits)){
                    #加上支出价格 利润统计log_profits
                    Db::table("log_profits")->where("create_time  = '$time'")->setInc("pay_price",$pay_money);
                    #利润 = 原来的利润 减掉支出价格
                    Db::table("log_profits")->where("create_time  = '$time'")->setDec("total_price",$pay_money);
                }else{
                    #利润 = 收入-支出
                    $total_price = 0-intval($pay_money);
                    Db::table("log_profits")->insertGetId(array("create_time"=>$time,"pay_price"=>$pay_money,"total_price"=>$total_price));
                }




                #自动在资金池内扣除
                return array("status"=>true,'msg'=>"操作成功");
            }

        }else{
            #供应商结款表的id  supplier_fee
            $id = input("get.id");
            if(!empty($id)){
                #查询供应商应结款金额
                $info = Db::table("supplier_fee")->field("supplier_id,convert(coalesce(pay_price,0),decimal(10,2)) pay_price")->where("id = $id")->find();
                $this->assign('info',$info);
                $this->assign('id',$id);
                return $this->fetch();
            }
        }
    }
    /*
   * @param null
   * @content 供应商结款列表删除
   * @return array
   * */
    function del(){
        $id = input("post.id");
        if(!empty($id)){
            $res = Db::table("supplier_fee")->where("id = $id")->update(array("status"=>2));
            if($res){
                return array("status"=>true,'msg'=>"操作成功");
            }

        }
    }
    /*
     * @param null
     * @content 结款列表
     * @return array
     * */
    function fee_index(){
        if(input("get.active") == "ajax"){
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'),true);
            $_where = "";
            if (!empty($searchParams)) {
                if (!empty($searchParams['supplier'])) {
                    $_where .= "sf.supplier_id = '".$searchParams['supplier']."'";
                }
            }

            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('supplier_fee sf')
                ->field("s.supplier_name,s.relation_phone,s.account_name,s.bank_account,s.open_bank,s.bank_address,
                (select title from bizpro_class where id in (s.bizpro_class)) bizpro_class,
                sf.*")
                ->join("supplier s",'sf.supplier_id = s.id')
                ->order("id desc")
                ->where("status = 1")
                ->where($_where)
                ->paginate($listRows, false, ['query' => request()->param()]);

            return $this->jsonSuccessData($info);
        }else{
            $supplier = Db::table("supplier")->select();
            $this->assign('supplier',$supplier);
            return $this->fetch();
        }
    }
    /*
   * @param null
   * @content 结款记录
   * @return array
   * */
    function log_fee(){
        $id = input("get.id");
        if(input("get.active") == "ajax"){
            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            #查询结款记录
            $pay = Db::table("log_fee")->where("sfee_id = $id")->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($pay);
        }else{
            $this->assign('id',$id);
            return $this->fetch();
        }
    }
    /*
   * @param null
   * @content 资金管理页面
   * @return array
   * */
    function poll_index(){
        #查询金额
        $info = Db::table("poll")->find();
        $this->assign('wx_price',getPriceInfo($info['wx_price']));
        $this->assign('alipay_price',getPriceInfo($info['alipay_price']));
        return $this->fetch();
    }
    /*
   * @param null
   * @content 资金管理结算记录
   * @return array
   * */
    function logPayment(){

        if(input("get.active") == "ajax"){
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'),true);
            $_where = "";
            if (!empty($searchParams)) {
                if (!empty($searchParams['pay_type'])) {
                    $_where .= "pay_type = '".$searchParams['pay_type']."'";
                }
            }
            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('log_poll')
                ->where($_where)
                ->paginate($listRows, false, ['query' => request()->param()]);

            return $this->jsonSuccessData($info);
        }else{

            return $this->fetch();
        }
    }
    /*
     * @param null
     * @content 资金结算
     * @return array
     * */
    function payment(){
        if(input("get.active") == "ajax"){
            $info = input("post.");
            $info['create_time'] = date("Y-m-d H:i:s");
            if(!empty($info)){
                #查询资金池里的钱是否够
                $poll = Db::table("poll")->find();
                if($info['pay_type'] == 1 ){
                    #微信
                    if(intval($info['price'])>$poll['wx_price']){
                        return array("status"=>false,'msg'=>"结款金额大于资金池库存金额,无法结算");
                    }else{
                        #加结款记录
                        $res = Db::table("log_poll")->insert($info);
                        if($res){
                            #扣除资金池内金额
                            #微信
                            Db::table("poll")->where("id = 1")->setDec("wx_price",$info['price']);
                            return array("status"=>true);
                        }else{
                            return array("status"=>false,'msg'=>"结款记录没加上");
                        }

                    }
                }else{
                    #支付宝 结款金额>资金库存金额  无法结算
                    if(intval($info['price'])>$poll['alipay_price']){
                        return array("status"=>false,'msg'=>"结款金额大于资金池库存金额,无法结算");
                    }else{
                        #加结款记录
                        $res = Db::table("log_poll")->insert($info);
                        if($res){
                            #扣除资金池内金额
                            #支付宝
                            Db::table("poll")->where("id = 1")->setDec("alipay_price",$info['price']);
                            return array("status"=>true);
                        }else{
                            return array("status"=>false,'msg'=>"结款记录没加上");

                        }
                    }
                }

            }
        }else{
            return $this->fetch();
        }
    }
}